#    Copyright 2012 New Mexico EPSCoR
#
#    This file is part of Teacher's P.E.T.
#
#    Teacher's P.E.T. is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Teacher's P.E.T. is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Teacher's P.E.T..  If not, see <http://www.gnu.org/licenses/>.
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
from apps.forum.models import *
from apps.course.functions import course_reverse

from base import ForumTestCaseBase, ForumMessageTestCaseBase, ForumThreadTestCaseBase

def management_url(course=None,app='forum',command='manage',**kwargs):
    return course_reverse('management',course=course,app=app,command=command,**kwargs)

class ForumManagementManageTestCases(ForumTestCaseBase):
    """
    - Check that tagged items appear properly:
      - TaggedEntries
	  - TaggedThreads
	  - TaggedMessages
	  - BlockList
	  - Bans
      - AttachForm if no forum
    """
    def test_attach_form(self):
        #Attach forum as non-instructor
        response = self.instructor1.client.post(management_url())
        self.assertIsNotNone(response.context['AttachForm'])
    def test_no_attach_form(self):
        #Attach forum as non-instructor
        response = self.instructor1.client.post(management_url(course=self.course1))
        self.assertIsNone(response.context['AttachForm'])
        
    def test_tagged_items(self):
        pass

class ForumManagementAttachForumTestCases(ForumTestCaseBase):
    """
    - Check for error on bad forum id
    - Check for error if forum already attached
    - Check for success
    """
    def test_attach_error_non_instructor(self):
        #Attach forum as non-instructor
        response = self.instructor2.client.post(management_url(command='attach'),{'forum':self.course1.forum.id})
        self.assertEqual(response.status_code,404)
        self.assertEqual(Course.find(None).forum,None)
        
    def test_attach_error_bad_forum_id(self):
        #Attach bad forum as instructor
        response = self.instructor1.client.post(management_url(command='attach'),{'forum':9999})
        self.assertEqual(response.status_code,200)
        self.assertEqual(Course.find(None).forum,None)
        self.assertInMessages(response,"Error attaching forum")
        
    def test_attach_error_junk_data(self):
        #Attach bad forum as instructor
        response = self.instructor1.client.post(management_url(command='attach'),{'junk_data':'Look at me! I\'m on tv!'})
        self.assertEqual(response.status_code,302)
        self.assertRedirects(response,management_url())
        self.assertEqual(Course.find(None).forum,None)

    def test_attach_success(self):
        #Attach forum as instructor
        response = self.instructor1.client.post(management_url(command='attach'),{'forum':self.course1.forum.id})
        self.assertRedirects(response,management_url())
        self.assertNotEqual(Course.find(None).forum,None)
        
    def test_attach_error_exists(self):
        #Attach forum as instructor
        response = self.instructor1.client.post(management_url(command='attach'),{'forum':self.course1.forum.id})
        #Reattach the forum
        response = self.instructor1.client.post(management_url(command='attach'),{'forum':self.course1.forum.id})
        self.assertRedirects(response,management_url())
        self.assertIn("already exists",response.cookies['messages'].value)

class ForumManagementCreateNewForumTestCases(ForumTestCaseBase):
    """
    - Check for error on blank title
    - Check for error if forum exists
    - Check for success
    """
    def test_create_error_non_instructor(self):
        #Create forum as non-instructor
        response = self.instructor2.client.post(management_url(command='create'),{'title':'I am a mighty test forum'})
        self.assertEqual(response.status_code,404)
        self.assertEqual(Course.find(None).forum,None)
        
    def test_create_error_no_title(self):
        #Create titleless forum as instructor
        response = self.instructor1.client.post(management_url(command='create'),{'title':''})
        self.assertEqual(response.status_code,200)
        self.assertEqual(Course.find(None).forum,None)
        self.assertInMessages(response,"title is required")
        
    def test_create_error_junk_data(self):
        #Create titleless forum as instructor
        response = self.instructor1.client.post(management_url(command='create'),{'junk-data':'lollipop guild membership confirmed'})
        self.assertRedirects(response,management_url())
        self.assertEqual(Course.find(None).forum,None)
        
    def test_create_success(self):
        #Create forum as instructor
        response = self.instructor1.client.post(management_url(command='create'),{'title':'I am a mighty test forum'})
        self.assertRedirects(response,management_url())
        self.assertNotEqual(Course.find(None).forum,None)
        
    def test_create_error_exists(self):
        #Create forum as instructor
        response = self.instructor1.client.post(management_url(command='create'),{'title':'I am a mighty test forum'})
        #recreate the same forum again
        response = self.instructor1.client.post(management_url(command='create'),{'title':'I am a mighty test forum'})
        self.assertRedirects(response,management_url())
        self.assertIn("already exists",response.cookies['messages'].value)

class ForumManagementDeleteTestCases(ForumTestCaseBase):
    """
    - Check that a redirect to forum management occurs if no forum
    - Check that a confirmation dialog is shown if no POST
    - Check success message, redirect to forum management
    - Check forum removal (but not deletion), redirect to forum management
    """
    def test_delete_error_no_forum(self):
        response = self.instructor1.client.post(management_url(command='delete'))
        self.assertIn("nothing to delete",response.cookies['messages'].value)
        self.assertRedirects(response,management_url())
    
    def test_delete_confirmation(self):
        response = self.instructor1.client.get(management_url(self.course1,command='delete'))
        self.assertEqual(response.status_code,200)
        self.assertTemplateUsed(response,"forum/management/delete.html")
    
    def test_delete_removal(self):
        response = self.instructor1.client.post(management_url(command='attach'),{'forum':self.course1.forum.id})
        self.assertIsNotNone(Course.find(None).forum)
        response = self.instructor1.client.post(management_url(self.course1,command='delete'))
        self.assertIn("removed",response.cookies['messages'].value)
        self.assertRedirects(response,management_url(self.course1))
        
    def test_delete_success(self):
        response = self.instructor1.client.post(management_url(self.course1,command='delete'))
        self.assertIn("deleted",response.cookies['messages'].value)
        self.assertRedirects(response,management_url(self.course1))
        
class ForumManagementAlterMessageTestCases(ForumMessageTestCaseBase):
    """
    - Check that bad message results in 404
    - Check each option for success/proper message on repeat:
      - unflag
      - hide
      - unhide
    - Check that unknown option results in correct message
    """
    def test_bad_message(self):
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='99999&dontcare'))
        self.assertEqual(response.status_code,404)
        
    def test_bad_option(self):
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&dontcare'%self.msg1.id))
        self.assertInMessages(response,'Invalid option selected')
        self.assertRedirects(response,management_url())
        
    def test_unflag_success(self):
        self.msg1.mark_flagged_for_moderator(); self.msg1.save()
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&unflag'%self.msg1.id))
        self.assertInMessages(response,"Message unflagged")
        self.assertRedirects(response,management_url())
        msg = ForumPrivateMessage.objects.get(pk=self.msg1.id)
        self.assertTrue(msg.is_marked_for_moderator(False))
        
    def test_unflag_failure(self):
        self.msg1.mark_flagged_for_moderator(); self.msg1.save()
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&unflag'%self.msg1.id))
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&unflag'%self.msg1.id))
        self.assertInMessages(response,"Message not flagged")
        self.assertRedirects(response,management_url())
        msg = ForumPrivateMessage.objects.get(pk=self.msg1.id)
        self.assertTrue(msg.is_marked_for_moderator(False))
        
    def test_unhide_success(self):
        self.msg1.mark_hidden(); self.msg1.save()
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&unhide'%self.msg1.id))
        self.assertInMessages(response,"Message unhidden")
        self.assertRedirects(response,management_url())
        msg = ForumPrivateMessage.objects.get(pk=self.msg1.id)
        self.assertTrue(msg.is_hidden(False))
        
    def test_unhide_failure(self):
        self.msg1.mark_hidden(); self.msg1.save()
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&unhide'%self.msg1.id))
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&unhide'%self.msg1.id))
        self.assertInMessages(response,"Message hadn't been hidden")
        self.assertRedirects(response,management_url())
        msg = ForumPrivateMessage.objects.get(pk=self.msg1.id)
        self.assertTrue(msg.is_hidden(False))
        
    def test_hide_success(self):
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&hide'%self.msg1.id))
        self.assertInMessages(response,"Message hidden")
        self.assertRedirects(response,management_url())
        msg = ForumPrivateMessage.objects.get(pk=self.msg1.id)
        self.assertTrue(msg.is_hidden(True))
        
    def test_hide_failure(self):
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&hide'%self.msg1.id))
        response = self.instructor1.client.post(management_url(command='alter_message',arguments='%s&hide'%self.msg1.id))
        self.assertInMessages(response,"Message already hidden")
        self.assertRedirects(response,management_url())
        msg = ForumPrivateMessage.objects.get(pk=self.msg1.id)
        self.assertTrue(msg.is_hidden(True))

class ForumManagementAlterEntryTestCases(ForumThreadTestCaseBase):
    """
    - Check that bad entry results in 404
    - Check each option for success/proper message on repeat:
      - unflag
      - hide
      - unhide
    - Check that unknown option results in correct message
    """
    def test_bad_entry(self):
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='99999&dontcare'))
        self.assertEqual(response.status_code,404)
        
    def test_entry_bad_option(self):
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&dontcare'%self.thread1.entry1.id))
        self.assertInMessages(response,'Invalid option selected')
        self.assertRedirects(response,management_url())
        
    def test_entry_unflag_success(self):
        self.thread1.entry1.mark_flagged_for_moderator(); self.thread1.entry1.save()
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&unflag'%self.thread1.entry1.id))
        self.assertInMessages(response,"unflagged")
        self.assertRedirects(response,management_url())
        entry = ForumEntry.objects.get(pk=self.thread1.entry1.id)
        self.assertTrue(entry.is_marked_for_moderator(False))
        
    def test_entry_unflag_failure(self):
        self.thread1.entry1.mark_flagged_for_moderator(); self.thread1.entry1.save()
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&unflag'%self.thread1.entry1.id))
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&unflag'%self.thread1.entry1.id))
        self.assertInMessages(response,"not flagged")
        self.assertRedirects(response,management_url())
        entry = ForumEntry.objects.get(pk=self.thread1.entry1.id)
        self.assertTrue(entry.is_marked_for_moderator(False))
        
    def test_entry_unhide_success(self):
        self.thread1.entry1.mark_hidden(); self.thread1.entry1.save()
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&unhide'%self.thread1.entry1.id))
        self.assertInMessages(response,"unhidden")
        self.assertRedirects(response,management_url())
        entry = ForumEntry.objects.get(pk=self.thread1.entry1.id)
        self.assertTrue(entry.is_hidden(False))
        
    def test_entry_unhide_failure(self):
        self.thread1.entry1.mark_hidden(); self.thread1.entry1.save()
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&unhide'%self.thread1.entry1.id))
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&unhide'%self.thread1.entry1.id))
        self.assertInMessages(response,"hadn't been hidden")
        self.assertRedirects(response,management_url())
        entry = ForumEntry.objects.get(pk=self.thread1.entry1.id)
        self.assertTrue(entry.is_hidden(False))
        
    def test_entry_hide_success(self):
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&hide'%self.thread1.entry1.id))
        self.assertInMessages(response,"hidden")
        self.assertRedirects(response,management_url())
        entry = ForumEntry.objects.get(pk=self.thread1.entry1.id)
        self.assertTrue(entry.is_hidden(True))
        
    def test_entry_hide_failure(self):
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&hide'%self.thread1.entry1.id))
        response = self.instructor1.client.post(management_url(command='alter_entry',arguments='%s&hide'%self.thread1.entry1.id))
        self.assertInMessages(response,"already hidden")
        self.assertRedirects(response,management_url())
        entry = ForumEntry.objects.get(pk=self.thread1.entry1.id)
        self.assertTrue(entry.is_hidden(True))

class ForumManagementAlterThreadTestCases(ForumThreadTestCaseBase):
    """
    - Check that bad thread results in 404
    - Check if src=='view' that it sends a 302 to forum_view_entries for the thread
    - Check if src!='view' that it returns to forum management
    - Check each option for success/proper message on repeat:
      - sticky
      - unsticky
      - hide
      - unhide
      - lock
      - unlock
    - Check that unknown option results in correct message
    """
    def test_bad_thread(self):
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='99999&dontcare&management'))
        self.assertEqual(response.status_code,404)
        
    def test_thread_bad_option(self):
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&dontcare&management'%self.thread1.id))
        self.assertInMessages(response,'Invalid option selected')
        self.assertRedirects(response,management_url(course=self.course1))
    
    def test_thread_src_view(self):
        self.thread1.mark_hidden(); self.thread1.save()
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unhide&view'%self.thread1.id))
        self.assertRedirects(response,course_reverse('forum_view_entries',self.course1,thread_id=self.thread1.id))
        
    def test_thread_unhide_success(self):
        self.thread1.mark_hidden(); self.thread1.save()
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unhide&management'%self.thread1.id))
        self.assertInMessages(response,"unhidden")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_hidden(False))
        
    def test_thread_unhide_failure(self):
        self.thread1.mark_hidden(); self.thread1.save()
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unhide&management'%self.thread1.id))
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unhide&management'%self.thread1.id))
        self.assertInMessages(response,"hadn't been hidden")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_hidden(False))
        
    def test_thread_hide_success(self):
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&hide&management'%self.thread1.id))
        self.assertInMessages(response,"hidden")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_hidden(True))
        
    def test_thread_hide_failure(self):
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&hide&management'%self.thread1.id))
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&hide&management'%self.thread1.id))
        self.assertInMessages(response,"already hidden")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_hidden(True))
        
    def test_thread_unsticky_success(self):
        self.thread1.mark_sticky(); self.thread1.save()
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unsticky&management'%self.thread1.id))
        self.assertInMessages(response,"unstickied")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_sticky(False))
        
    def test_thread_unsticky_failure(self):
        self.thread1.mark_sticky(); self.thread1.save()
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unsticky&management'%self.thread1.id))
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unsticky&management'%self.thread1.id))
        self.assertInMessages(response,"hadn't been sticky")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_sticky(False))
        
    def test_thread_sticky_success(self):
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&sticky&management'%self.thread1.id))
        self.assertInMessages(response,"marked sticky")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_sticky(True))
        
    def test_thread_sticky_failure(self):
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&sticky&management'%self.thread1.id))
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&sticky&management'%self.thread1.id))
        self.assertInMessages(response,"already marked sticky")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_sticky(True))
        
    def test_thread_unlocked_success(self):
        self.thread1.mark_locked(); self.thread1.save()
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unlock&management'%self.thread1.id))
        self.assertInMessages(response,"unlocked")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_locked(False))
        
    def test_thread_unlocked_failure(self):
        self.thread1.mark_locked(); self.thread1.save()
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unlock&management'%self.thread1.id))
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&unlock&management'%self.thread1.id))
        self.assertInMessages(response,"hadn't been locked")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_locked(False))
        
    def test_thread_locked_success(self):
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&lock&management'%self.thread1.id))
        self.assertInMessages(response,"locked")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_locked(True))
        
    def test_thread_locked_failure(self):
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&lock&management'%self.thread1.id))
        response = self.instructor1.client.post(management_url(course=self.course1,command='alter_thread',arguments='%s&lock&management'%self.thread1.id))
        self.assertInMessages(response,"already locked")
        self.assertRedirects(response,management_url(course=self.course1))
        thread = ForumThread.objects.get(pk=self.thread1.id)
        self.assertTrue(thread.is_locked(True))

class ForumManagementBanUserTestCases(ForumTestCaseBase):
    """
    - Check that a bad course_participant id results in a 302 to forum management
    - Check that bad info in the BanForm results in showing forum/management/ban.html again (with errors)
    - Check that ban gets deleted if all restrictions removed
    - Check that success results in a 302 to forum management
    """
    def setUp(self):
        super(ForumManagementBanUserTestCases,self).setUp()
        self.data = {
            'forum_ban':ForumBan.NO_BAN,
            'message_ban':ForumBan.NO_BAN,
        }
    def test_ban_success(self):
        self.data['forum_ban'] = ForumBan.BAN_POSTS; self.data['message_ban'] = ForumBan.BAN_MESSAGE_ATTACHMENTS
        self.assertEqual(list(ForumBan.objects.filter(course_participant=self.student1.id,forum=self.course1.forum)),[])
        response = self.instructor1.client.post(management_url(self.course1,command='ban',arguments=self.student1.id),self.data)
        self.assertRedirects(response,management_url(self.course1))
        self.assertNotEqual(list(ForumBan.objects.filter(course_participant=self.student1.id,forum=self.course1.forum)),[])
        Ban = ForumBan.objects.filter(course_participant=self.student1.id,forum=self.course1.forum)[0]
        self.assertEqual(Ban.forum_ban,self.data['forum_ban'])
        self.assertEqual(Ban.message_ban,self.data['message_ban'])
        
    def test_ban_bad_data(self):
        self.data['forum_ban'] = ForumBan.BAN_MESSAGE_ATTACHMENTS; self.data['message_ban'] = -1567492
        response = self.instructor1.client.post(management_url(self.course1,command='ban',arguments=self.student1.id),self.data)
        self.assertTemplateUsed(response,"forum/management/ban.html")
        self.assertInFormError(response,'BanForm','forum_ban','Select a valid choice')
        self.assertInFormError(response,'BanForm','message_ban','Select a valid choice')
        
    def test_ban_deleted(self):
        del_data = self.data.copy(); del_data['forum_ban'] = ForumBan.BAN_POSTS; del_data['message_ban'] = ForumBan.BAN_MESSAGE_ATTACHMENTS
        self.assertEqual(list(ForumBan.objects.filter(course_participant=self.student1.id,forum=self.course1.forum)),[])
        response = self.instructor1.client.post(management_url(self.course1,command='ban',arguments=self.student1.id),del_data)
        self.assertNotEqual(list(ForumBan.objects.filter(course_participant=self.student1.id,forum=self.course1.forum)),[])
        response = self.instructor1.client.post(management_url(self.course1,command='ban',arguments=self.student1.id),self.data)
        self.assertEqual(list(ForumBan.objects.filter(course_participant=self.student1.id,forum=self.course1.forum)),[])
    
    def test_ban_bad_course_participant_id(self):
        response = self.instructor1.client.post(management_url(self.course1,command='ban',arguments=9989873345),self.data)
        self.assertRedirects(response,management_url(self.course1))
        self.assertInMessages(response,"User not found for this course")

class ForumManagementViewMessagesTestCases(ForumMessageTestCaseBase):
    """
    - Check that no drafts appear
    - Check that deleted messages are included
    - Check that bad username results in 404
    - Check that messages are only from the sent username
    - Check that a bad page defaults to the first page
    - Check that a huge page defaults to the last page
    """
    def test_view_messages_huge_page(self):
        response = self.instructor1.client.post(management_url(self.course1,command='view_messages',arguments="%s&9999" % (self.student1.user.username)))
        self.assertTrue(response.context)
        self.assertTrue('page' in response.context)
        self.assertTrue(response.context['page'])
        self.assertEqual(response.context['page'].number,response.context['page'].paginator.num_pages)
        
    def test_view_messages_bad_page(self):
        response = self.instructor1.client.post(management_url(self.course1,command='view_messages',arguments="%s&gooberfish" % (self.student1.user.username)))
        self.assertTrue(response.context)
        self.assertTrue('page' in response.context)
        self.assertTrue(response.context['page'])
        self.assertEqual(response.context['page'].number,response.context['page'].paginator.num_pages)
        
    def test_view_messages_no_drafts(self):
        response = self.instructor1.client.post(management_url(self.course1,command='view_messages',arguments=self.student1.user.username))
        self.assertTrue(response.context)
        self.assertTrue('page' in response.context)
        self.assertTrue(response.context['page'])
        for msg in response.context['page'].object_list:
            self.assertTrue(msg.is_sent())
    
    def test_view_messages_bad_username(self):
        response = self.instructor1.client.post(management_url(self.course1,command='view_messages',arguments="oadsfhbewnopfhviijsnbjsaoijsgfbngfd"))
        self.assertEqual(response.status_code, 404)
        
    def test_view_messages_correct_username(self):
        response = self.instructor1.client.post(management_url(self.course1,command='view_messages',arguments=self.student1.user.username))
        self.assertTrue(response.context)
        self.assertTrue('page' in response.context)
        self.assertTrue(response.context['page'])
        for msg in response.context['page'].object_list:
            self.assertEqual(msg.sender,self.student1.user)
            
    def test_view_messages_include_deleted(self):
        #there should be at least one deleted message in the set created in setUp
        response = self.instructor1.client.post(management_url(self.course1,command='view_messages',arguments=self.student1.user.username))
        self.assertTrue(response.context)
        self.assertTrue('page' in response.context)
        self.assertTrue(response.context['page'])
        found_deleted = False
        for msg in response.context['page'].object_list:
            if msg.is_deleted(): found_deleted = True
        self.assertTrue(found_deleted)
        
        

